{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 导入基本包\n",
    "import os\n",
    "import re\n",
    "import jieba\n",
    "import numpy as py\n",
    "import pandas as pd\n",
    "import pyprind\n",
    "from bs4 import BeautifulSoup\n",
    "from nltk.corpus import stopwords\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "from nltk.stem.porter import PorterStemmer\n",
    "from sklearn.naive_bayes import MultinomialNB\n",
    "from sklearn import metrics\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.naive_bayes import BernoulliNB\n",
    "from sklearn.preprocessing import LabelEncoder"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.加载数据"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "0% [##############################] 100% | ETA: 00:00:00\n",
      "Total time elapsed: 00:00:53\n"
     ]
    }
   ],
   "source": [
    "# 加载数据集\n",
    "# 数据加载\n",
    "path = 'data/aclImdb'\n",
    "labels = {'pos': 1, 'neg': 0}\n",
    "# 设置进度条\n",
    "pbar = pyprind.ProgBar(25000)\n",
    "\n",
    "\n",
    "def getData(type):\n",
    "    \"\"\"\n",
    "    数据加载，将原数据顺序打乱\n",
    "    :param: 需要加载的数据集类型train、test\n",
    "    :return: DataFrame\n",
    "    \"\"\"\n",
    "    data = pd.DataFrame()\n",
    "    for j in ('pos', 'neg'):\n",
    "        # 拼接路径\n",
    "        type_path = os.path.join(path, type, j)\n",
    "        for k in os.listdir(type_path):\n",
    "            # 以文件流的形式打开文件\n",
    "            with open(os.path.join(type_path, k), 'r', encoding='utf-8') as file:\n",
    "                comment = file.read()\n",
    "            data = data.append([[comment, labels[j]]], ignore_index=True)\n",
    "            pbar.update()\n",
    "    data.columns = ['comment', 'sentiment']\n",
    "    # 原数据规律性太强，将原数据顺序打乱\n",
    "    resultData = data.sample(frac=1).reset_index(drop=True)\n",
    "    return resultData\n",
    "\n",
    "\n",
    "train_data = getData(\"train\")\n",
    "test_data = getData(\"test\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                             comment  sentiment\n",
      "0  Now this is the sort of film we used to get we...          1\n",
      "1  <br /><br />Emilio Estevez takes the wonderful...          1\n",
      "2  This was a letdown in many ways. The location ...          0\n",
      "3  This is actually a very good surreal mystery m...          1\n",
      "4  ...Or better yet, watch Fandango if you want t...          0\n",
      "5  This film is really quite odd. Clearly certain...          0\n",
      "6  cool flick. enjoyable to watch. hope to see mo...          1\n",
      "7  this movie is one that belongs on the cutting ...          0\n",
      "8  After seeing the movie in a class of mine and ...          0\n",
      "9  If you like your sports movies to be about dig...          1\n"
     ]
    }
   ],
   "source": [
    "print(test_data[0:10])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 数据预处理"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "source": [
    "def html_to_text(sentence):\n",
    "    \"\"\"\n",
    "    因为评论来源于爬虫抓取，因此可能会携带Html标签，\n",
    "    本函数的作用是使用bs4库去除文本中的HTML标签及非法字符\n",
    "    :param sentence: str\n",
    "    :return: str\n",
    "    \"\"\"\n",
    "    # 把大写转化为小写\n",
    "    sentence = sentence.lower()\n",
    "    # sentence = re.sub(\"<br />\", \" \", sentence)\n",
    "\n",
    "    # 去除Html标签\n",
    "    soup = BeautifulSoup(sentence, 'lxml')\n",
    "    sentence = soup.get_text()\n",
    "\n",
    "    return sentence\n",
    "\n",
    "\n",
    "def tokenizer(sentence):\n",
    "    \"\"\"\n",
    "    分词操作，由于是英文，直接用空格做切分即可，对于中文分词用结巴分词库来操作\n",
    "    :param sentence: str\n",
    "    :return: [word,word,word]\n",
    "    \"\"\"\n",
    "    # 定义正则过滤器\n",
    "    filters = ['!', '\"', '#', '$', '%', '&', '\\(', '\\)', '\\*', '\\+', ',', '-', '\\.', '/', ':', ';', '<', '=', '>',\n",
    "               '\\?', '@', '\\[', '\\\\', '\\]', '^', '_', '`', '\\{', '\\|', '\\}', '~', '\\t', '\\n', '\\x97', '\\x96', '”',\n",
    "               '“', ]\n",
    "    sentence = re.sub(\"|\".join(filters), \" \", sentence)\n",
    "\n",
    "    result = [i for i in sentence.split(\" \") if len(i) > 0]\n",
    "    return result\n",
    "\n",
    "\n",
    "def original_word(sentence_list):\n",
    "    \"\"\"\n",
    "    使用PorterStemmer还原词干\n",
    "    :param sentence_list: [running,word,words]\n",
    "    :return: [running,word,word]\n",
    "    \"\"\"\n",
    "    # 还原词根\n",
    "    word_list = []\n",
    "    porter = PorterStemmer()\n",
    "    for word in sentence_list:\n",
    "        sentence = porter.stem(word)\n",
    "        word_list.append(sentence)\n",
    "    return word_list\n",
    "\n",
    "\n",
    "def remove_stopwords(sentence_list):\n",
    "    \"\"\"\n",
    "    去除停用词\n",
    "    :param sentence_list: [str,str,str]\n",
    "    :return: [str,str,str]\n",
    "    \"\"\"\n",
    "    filtered_words = []\n",
    "    # 英文停用词\n",
    "    stop_words = stopwords.words('english')\n",
    "    for filters in ['!', ',', '.', '?', '-s', '-ly', '</s>', 's', '1', '2', '3', '4', '5', '6', '7', '8', '9']:\n",
    "        stop_words.append(filters)\n",
    "    for word in sentence_list:\n",
    "        if word not in stop_words:\n",
    "            filtered_words.append(word)\n",
    "    return filtered_words\n",
    "\n",
    "\n",
    "def get_sentence(text):\n",
    "    \"\"\"\n",
    "     获取处理好的数据\n",
    "    :param text: [str,str,str]\n",
    "    :return: [str,str,str]\n",
    "    \"\"\"\n",
    "\n",
    "    # 文本清洗\n",
    "    sentence = html_to_text(text)\n",
    "    # 文本分词\n",
    "    tokenizer_list = tokenizer(sentence)\n",
    "    # 去除停用词\n",
    "    words_list = remove_stopwords(tokenizer_list)\n",
    "    # 还原词干\n",
    "    #finall_word_list=pro.original_word(words_list)\n",
    "\n",
    "    return words_list"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "execution_count": 4,
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Appinstall\\Anaconda\\lib\\site-packages\\pandas\\core\\indexing.py:670: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  iloc._setitem_with_indexer(indexer, value)\n",
      "0% [##############################] 100% | ETA: 00:00:00\n",
      "Total time elapsed: 00:01:01\n",
      "0% [##############################] 100% | ETA: 00:00:00\n",
      "Total time elapsed: 00:01:01\n"
     ]
    }
   ],
   "source": [
    "def wordCut(data):\n",
    "    \"\"\"\n",
    "    提取train_data，test_data中的Comment数据，并将每条Comment进行清洗、分词、去除停用词、还原词干等操作\n",
    "    最终将分词后的训练集数据或测试数据集中的comment分别添加到一个列表中\n",
    "\n",
    "    :param: DataFrame  train_data,test_data\n",
    "    :return: [[str,str,str],[str,str,str],[str,str,str]]\n",
    "    \"\"\"\n",
    "    # 存放分词、清洗等操作后的Comment\n",
    "    sentences = []\n",
    "    bar = pyprind.ProgBar(len(data))\n",
    "    for i in range(0, len(data)):\n",
    "        data['comment'].iloc[i] = get_sentence(data['comment'].iloc[i])\n",
    "        sentences.append(data['comment'].iloc[i])\n",
    "        bar.update()\n",
    "    return sentences\n",
    "\n",
    "\n",
    "# all_words=train_sentences+test_sentences\n",
    "# 分词后的训练集\n",
    "train_sentences = wordCut(train_data)\n",
    "# 分词后的测试集\n",
    "test_sentences = wordCut(test_data)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25000\n",
      "25000\n"
     ]
    }
   ],
   "source": [
    "print(len(train_sentences))\n",
    "print(len(test_sentences))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 特征向量化"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 1.标签规格化"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "0% [##############################] 100% | ETA: 00:00:00\n",
      "Total time elapsed: 00:00:00\n",
      "0% [##############################] 100% | ETA: 00:00:00\n",
      "Total time elapsed: 00:00:00\n"
     ]
    }
   ],
   "source": [
    "\n",
    "def getLabels(data):\n",
    "    \"\"\"\n",
    "    将train_data、test_data中的label标签提取出来,并将其格式化处理\n",
    "    :param: DataFrame  train_data,test_data\n",
    "    :return: [0 1 0 1 1 1 0 .....]\n",
    "    \"\"\"\n",
    "    label = []\n",
    "    bar = pyprind.ProgBar(len(data))\n",
    "    # 合并label\n",
    "    for i in range(0, len(data)):\n",
    "        label.append(data['sentiment'].iloc[i])\n",
    "        bar.update()\n",
    "    # 将label进行规格化\n",
    "    le = LabelEncoder()\n",
    "    resultLabels = le.fit_transform(label)\n",
    "    return resultLabels\n",
    "\n",
    "train_Label = getLabels(train_data)\n",
    "test_Label = getLabels(test_data)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 1 1 0 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 0 0 1 0 0 0 1 1 0 0]\n",
      "[1 1 0 1 0 0 1 0 0 1 1 1 0 0 0 0 0 1 0 0 1 1 1 0 0 0 0 1 1 0]\n"
     ]
    }
   ],
   "source": [
    "print(train_Label[:30])\n",
    "print(test_Label[:30])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "# 将清洗后的单词再合并为一个句子\n",
    "train_c_text=list(map(lambda s: ' '.join(s),train_sentences))\n",
    "test_c_text=list(map(lambda s: ' '.join(s),train_sentences))\n",
    "# 句子向量化\n",
    "tfidf=TfidfVectorizer(binary=False,token_pattern=r\"(?u)\\b\\w+\\b\")\n",
    "train_Data=tfidf.fit_transform(train_c_text)\n",
    "test_Data=tfidf.transform(test_c_text)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[1;32m<ipython-input-12-1515fa05495e>\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[1;31m# 模型训练与测试\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      2\u001B[0m \u001B[0mmulti_model\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mGaussianNB\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m----> 3\u001B[1;33m \u001B[0mmulti_model\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mfit\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mtrain_Data\u001B[0m\u001B[1;33m,\u001B[0m\u001B[0mtrain_Label\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m      4\u001B[0m \u001B[0mpredict\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mmulti_model\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mpredict\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mtest_Data\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      5\u001B[0m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mpredict\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mD:\\Appinstall\\Anaconda\\lib\\site-packages\\sklearn\\naive_bayes.py\u001B[0m in \u001B[0;36mfit\u001B[1;34m(self, X, y, sample_weight)\u001B[0m\n\u001B[0;32m    208\u001B[0m         \u001B[0mself\u001B[0m \u001B[1;33m:\u001B[0m \u001B[0mobject\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    209\u001B[0m         \"\"\"\n\u001B[1;32m--> 210\u001B[1;33m         \u001B[0mX\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0my\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0m_validate_data\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mX\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0my\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    211\u001B[0m         \u001B[0my\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mcolumn_or_1d\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0my\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mwarn\u001B[0m\u001B[1;33m=\u001B[0m\u001B[1;32mTrue\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    212\u001B[0m         return self._partial_fit(X, y, np.unique(y), _refit=True,\n",
      "\u001B[1;32mD:\\Appinstall\\Anaconda\\lib\\site-packages\\sklearn\\base.py\u001B[0m in \u001B[0;36m_validate_data\u001B[1;34m(self, X, y, reset, validate_separately, **check_params)\u001B[0m\n\u001B[0;32m    430\u001B[0m                 \u001B[0my\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mcheck_array\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0my\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;33m**\u001B[0m\u001B[0mcheck_y_params\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    431\u001B[0m             \u001B[1;32melse\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 432\u001B[1;33m                 \u001B[0mX\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0my\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mcheck_X_y\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mX\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0my\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;33m**\u001B[0m\u001B[0mcheck_params\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    433\u001B[0m             \u001B[0mout\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mX\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0my\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    434\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mD:\\Appinstall\\Anaconda\\lib\\site-packages\\sklearn\\utils\\validation.py\u001B[0m in \u001B[0;36minner_f\u001B[1;34m(*args, **kwargs)\u001B[0m\n\u001B[0;32m     70\u001B[0m                           FutureWarning)\n\u001B[0;32m     71\u001B[0m         \u001B[0mkwargs\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mupdate\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m{\u001B[0m\u001B[0mk\u001B[0m\u001B[1;33m:\u001B[0m \u001B[0marg\u001B[0m \u001B[1;32mfor\u001B[0m \u001B[0mk\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0marg\u001B[0m \u001B[1;32min\u001B[0m \u001B[0mzip\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0msig\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mparameters\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0margs\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m}\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m---> 72\u001B[1;33m         \u001B[1;32mreturn\u001B[0m \u001B[0mf\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m**\u001B[0m\u001B[0mkwargs\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m     73\u001B[0m     \u001B[1;32mreturn\u001B[0m \u001B[0minner_f\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m     74\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mD:\\Appinstall\\Anaconda\\lib\\site-packages\\sklearn\\utils\\validation.py\u001B[0m in \u001B[0;36mcheck_X_y\u001B[1;34m(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, estimator)\u001B[0m\n\u001B[0;32m    793\u001B[0m         \u001B[1;32mraise\u001B[0m \u001B[0mValueError\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m\"y cannot be None\"\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    794\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 795\u001B[1;33m     X = check_array(X, accept_sparse=accept_sparse,\n\u001B[0m\u001B[0;32m    796\u001B[0m                     \u001B[0maccept_large_sparse\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0maccept_large_sparse\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    797\u001B[0m                     \u001B[0mdtype\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mdtype\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0morder\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0morder\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mcopy\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mcopy\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mD:\\Appinstall\\Anaconda\\lib\\site-packages\\sklearn\\utils\\validation.py\u001B[0m in \u001B[0;36minner_f\u001B[1;34m(*args, **kwargs)\u001B[0m\n\u001B[0;32m     70\u001B[0m                           FutureWarning)\n\u001B[0;32m     71\u001B[0m         \u001B[0mkwargs\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mupdate\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m{\u001B[0m\u001B[0mk\u001B[0m\u001B[1;33m:\u001B[0m \u001B[0marg\u001B[0m \u001B[1;32mfor\u001B[0m \u001B[0mk\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0marg\u001B[0m \u001B[1;32min\u001B[0m \u001B[0mzip\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0msig\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mparameters\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0margs\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m}\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m---> 72\u001B[1;33m         \u001B[1;32mreturn\u001B[0m \u001B[0mf\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m**\u001B[0m\u001B[0mkwargs\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m     73\u001B[0m     \u001B[1;32mreturn\u001B[0m \u001B[0minner_f\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m     74\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mD:\\Appinstall\\Anaconda\\lib\\site-packages\\sklearn\\utils\\validation.py\u001B[0m in \u001B[0;36mcheck_array\u001B[1;34m(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)\u001B[0m\n\u001B[0;32m    573\u001B[0m     \u001B[1;32mif\u001B[0m \u001B[0msp\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0missparse\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0marray\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    574\u001B[0m         \u001B[0m_ensure_no_complex_data\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0marray\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 575\u001B[1;33m         array = _ensure_sparse_format(array, accept_sparse=accept_sparse,\n\u001B[0m\u001B[0;32m    576\u001B[0m                                       \u001B[0mdtype\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mdtype\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mcopy\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mcopy\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    577\u001B[0m                                       \u001B[0mforce_all_finite\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mforce_all_finite\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mD:\\Appinstall\\Anaconda\\lib\\site-packages\\sklearn\\utils\\validation.py\u001B[0m in \u001B[0;36m_ensure_sparse_format\u001B[1;34m(spmatrix, accept_sparse, dtype, copy, force_all_finite, accept_large_sparse)\u001B[0m\n\u001B[0;32m    351\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    352\u001B[0m     \u001B[1;32mif\u001B[0m \u001B[0maccept_sparse\u001B[0m \u001B[1;32mis\u001B[0m \u001B[1;32mFalse\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 353\u001B[1;33m         raise TypeError('A sparse matrix was passed, but dense '\n\u001B[0m\u001B[0;32m    354\u001B[0m                         \u001B[1;34m'data is required. Use X.toarray() to '\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    355\u001B[0m                         'convert to a dense numpy array.')\n",
      "\u001B[1;31mTypeError\u001B[0m: A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array."
     ]
    }
   ],
   "source": [
    "# 模型训练与测试\n",
    "multi_model=GaussianNB()\n",
    "multi_model.fit(train_Data,train_Label)\n",
    "predict=multi_model.predict(test_Data)\n",
    "print(predict)\n",
    "print(test_Label)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率： 0.50516\n"
     ]
    }
   ],
   "source": [
    "print(\"准确率：\",metrics.accuracy_score(predict,test_Label))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}